package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.write;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;

public class WriteStrValue {
  //变量名 16个字节
  private String valueName;
  //变量成员数量 4个字节
  private Integer valueNum;
  private List<WriteValueMember> members;

  public WriteStrValue(String valueName, Integer valueNum, List<WriteValueMember> members) {
    this.valueName = valueName;
    this.valueNum = valueNum;
    this.members = members;
  }

  public byte[] toBytes() {
    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeBytes(ByteUtils.stringToBytes(valueName, 16));
    byteBuf.writeBytes(ByteUtils.intToBytes(valueNum, 1));
    for (WriteValueMember member : members) {
      byteBuf.writeBytes(member.toBytes());
    }
    int i = byteBuf.writerIndex();
    byte[] bytes = new byte[i];
    byteBuf.readBytes(bytes);

    return bytes;
  }


}
